# This Python file uses the following encoding: utf-8
import json
from bson import ObjectId
import time

#000009.sz->sz000009
def zhuanhuan(code):
    t = code.split('.')
    str = t[1].lower()+t[0]
    return str

#sz000009->000009.sz
def zhuanhuan2(code):
    return code[2:]+'.'+code[0:2].upper()


def getDate():
    return time.strftime("%Y-%m-%d", time.localtime())


def to_json2(df,orient='split'):
    df_json = df.to_json(orient = orient, force_ascii = False)
    return json.loads(df_json)


class JSONEncoder(json.JSONEncoder):
    '''
    解决TypeError: Object of type 'ObjectId' is not JSON serializable
    '''
    #ensure_ascii解决中文乱码问题，根据自己情况天假
    def __init__(self, ensure_ascii=False):
        super().__init__(ensure_ascii=False)

    def default(self, o):
        if isinstance(o, ObjectId):
            return str(o)
        return json.JSONEncoder.default(self, o)

def to_json(df):
    return json.dumps(df,ensure_ascii=False)

#抓取的数据,转换成日线数据
def cchToDaily(c):
    #日期转换
    v = c[31].split('-')
    date = v[0]+v[1]+v[2]

    c2 = float(c[2])    #今日开盘
    c3 = float(c[3])    #昨日收盘
    c4 = float(c[4])    #收盘,当前价格
    c5 = float(c[5])
    c6 = float(c[6])
    c9 = float(c[9])
    c10 = float(c[10])

    ed = round(c4-c3,2)             #涨跌额度(3-1),
    if c2 == 0:
        fd = 0
    else:
        fd = round(ed/c2*100,2)             #涨跌幅度(3-1)/2
    cjs = c9/100                    #成交手数8/100
    cje = c10/1000                  #成交额,千元9/1000
    code = zhuanhuan2(c[0])
    return [code,date,c2,c5,c6,c4,c3,ed,fd,cjs,cje]
